package com.rjpacket.supernews.app.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.rjpacket.supernews.app.R;

import java.util.HashMap;
import java.util.List;

//listView显示内容

/**
 * Created by Administrator on 2015/3/10 0010.
 */
public class SlidingPaneMenuListAdapter extends BaseAdapter {
    //接收收据的List集合
    private List<HashMap<String, Object>> list;
    //上下文
    private Context context;
    //List里面存的哈希表
    private HashMap<String, Object> map;

    //构造方法，用来初始化信息
    public SlidingPaneMenuListAdapter(
            Context context, List<HashMap<String, Object>> list) {

        this.list = list;

        this.context = context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        View ret = null;
        //将上下文布局转化为View
        LayoutInflater inflater = LayoutInflater.from(context);

        ret = inflater.inflate(R.layout.item_slidingmenu_listview, viewGroup, false);

        ViewHolder holder = (ViewHolder) ret.getTag();

        if (holder == null) {

            holder = new ViewHolder();

            holder.image = (ImageView) ret.findViewById(R.id.slidingMenu_listView_image);

            holder.txt = (TextView) ret.findViewById(R.id.slidingMenu_listView_txt);

            ret.setTag(holder);
        }

        map = list.get(i);

        holder.image.setImageResource((Integer) map.get("img"));

        holder.txt.setText((String) map.get("title"));

        return ret;
    }

    //减少重复findViewById的次数优化
    private class ViewHolder {
        private ImageView image;
        private TextView txt;
    }
}
